package com.piesat.quake.business.device.model;

import com.piesat.quake.business.device.mqtt.anno.ConvertField;
import com.piesat.quake.business.device.mqtt.anno.PipelineNode;
import com.piesat.quake.business.device.mqtt.convert.TimestampConvert;

/**
 * 注册响应
 * 设备下行 QoS1
 *
 * @author Gersy
 * @date 2023/2/14
 * @since 1.0
 */
@PipelineNode(preNodes = {BaseModel.class})
public class RegisterResponse extends BaseModel {
    public static final short SUCCESS=0;
    public static final short FAIL=-1;
    //注册报文时间戳	Double	8B	格林威治时间1970年01月01日00时00分00秒（北京时间1970年01月01日08时00分00秒）起至现在的总秒数，超过30秒，认为报文无效。
    @ConvertField(length = 8, convert = TimestampConvert.class)
    private long registerTimestamp;
    //认证状态	Short③	2B	0认证成功；
    //-1认证失败；
    private short authenticatedState;

    public RegisterResponse() {
        super("rc");
    }

    public static RegisterResponse create(String id, int authenticatedState) {
        RegisterResponse registerResponse = new RegisterResponse();
        registerResponse.authenticatedState = (short) authenticatedState;
        long timestamp = System.currentTimeMillis();
        registerResponse.timestamp = timestamp;
        registerResponse.registerTimestamp = timestamp;
        registerResponse.setId(id);
        return registerResponse;
    }

    public long getRegisterTimestamp() {
        return registerTimestamp;
    }

    public void setRegisterTimestamp(long registerTimestamp) {
        this.registerTimestamp = registerTimestamp;
    }

    public short getAuthenticatedState() {
        return authenticatedState;
    }

    public void setAuthenticatedState(short authenticatedState) {
        this.authenticatedState = authenticatedState;
    }

    public void createRegisterTimestamp() {
        this.registerTimestamp = System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return "RegisterResponse{" +
                "registerTimestamp=" + registerTimestamp +
                ", authenticatedState=" + authenticatedState +
                ", type='" + type + '\'' +
                ", length=" + length +
                ", id='" + id + '\'' +
                ", endian=" + endian +
                ", timestamp=" + timestamp +
                '}';
    }
}
